package com.jsm.tf.net.protocol.serialize;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

@Slf4j
public class HessianSerializer implements Serializer {
    @Override
    public <T> T deserialize(Class<T> clazz, byte[] bytes) throws IOException {
        @Cleanup ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        @Cleanup Hessian2Input input = new Hessian2Input(bis);
        return (T) input.readObject();
    }

    @Override
    public <T> byte[] serialize(T object) throws IOException {
        @Cleanup   ByteArrayOutputStream bos = new ByteArrayOutputStream();
        @Cleanup  Hessian2Output oos = new Hessian2Output(bos);
        oos.writeObject(object);
        oos.flush();
        return bos.toByteArray();
    }
}
